package com.qianyu.project.web.controller.cyy;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qianyu.project.entity.*;
import com.qianyu.project.service.IAttachemenInfoService;
import com.qianyu.project.service.IBudgetService;
import com.qianyu.project.service.IDirectlyService;
import com.qianyu.project.service.IIndirectService;
import com.qianyu.project.utils.Result;
import com.qianyu.project.utils.ResultTool;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/budgets")
public class BudgetController {
    @Autowired
    private IDirectlyService directlyService;

    @Autowired
    private IBudgetService iBudgetService;

    @Autowired
    private IIndirectService iIndirectService;

    @GetMapping("")
    public Result getDirectlyList(@RequestParam Map<String,Object> map){
        List<Budget> bl = directlyService.getDirectlyList(map);
        bl.forEach(i->{
            List<Directly> dl = directlyService.list(new QueryWrapper<Directly>().eq("b_id", i.getI()));
            dl.forEach(ii->{
                if(ii.getCourseName().equals("1")) {
                    System.out.println(123);
                    i.setClMoney(ii.getMoney());
                }
                if(ii.getCourseName().equals("2")) {
                    i.setFbMoney(ii.getMoney());
                }
                if(ii.getCourseName().equals("3")) {
                    i.setZlMoney(ii.getMoney());
                }
                if(ii.getCourseName().equals("4")) {
                    i.setLwMoney(ii.getMoney());
                }
                 if(ii.getCourseName().equals("5")) {
                     i.setQtMoney(ii.getMoney());
                 }
            });
        });

        return ResultTool.success(bl);
    }

    @GetMapping("/aa")
    public Result getDirectlyList1(@RequestParam Map<String,Object> map){
        List<Budget> bl = directlyService.getDirectlyList1(map);

        return ResultTool.success(bl);
    }

    @GetMapping("/code")
    public Result getMaxCode() {
        System.out.println(123);
        return ResultTool.success(Long.parseLong(iBudgetService.getMaxCode()) + 1);
    }

    @GetMapping("/directly")
    public Result getDierctly(){
        return ResultTool.success(directlyService.getlist1());
    }

    @GetMapping("/indirect")
    public Result getIndirect(){
        return ResultTool.success(iIndirectService.getlist2());
    }

    @PostMapping("")
    public Result addLaborSubcontract(@RequestBody  Map<String,Object> adds){
        System.out.println(adds);
        Budget budget = new Budget();
        Map<String,Object> mapls = (Map<String, Object>) adds.get("subcontracts");
        budget.setYsId(Integer.valueOf(mapls.get("ysId")+""));
        budget.setBudgetName(mapls.get("budgetName")+"");
        budget.setPId(Integer.valueOf(mapls.get("pId")+""));
        budget.setEId(Integer.valueOf(mapls.get("eId")+""));
        budget.setCreateDate(mapls.get("createDate")+"");
        budget.setComment(mapls.get("comment")+"");
        budget.setAttachIds(mapls.get("attachIds")+"");
        budget.setStatus(Integer.valueOf(1+""));
        iBudgetService.saveOrUpdate(budget);
        List<Map<String,Object>> maptpd = (List<Map<String, Object>>) adds.get("tenderPurchaseDetails");
        List<Directly> aiList = Lists.newArrayList();
        maptpd.forEach(i->{
            Directly qq = new Directly();
            qq.setCourseName(i.get("courseName")+"");
            qq.setMoney(BigDecimal.valueOf(Double.valueOf(i.get("money")+"")));
            qq.setComment(i.get("comment")+"");
            qq.setMoneyCapital(i.get("moneyCapital")+"");
            qq.setBId(budget.getI());
            aiList.add(qq);
        });
        directlyService.saveBatch(aiList);
        return ResultTool.success();
    }

    @PutMapping("")
    public Result updateSubcontract(@RequestBody  Map<String,Object> adds){
        System.out.println(adds);
        Budget budget = new Budget();
        Map<String,Object> mapls = (Map<String, Object>) adds.get("subcontracts");
        budget.setI(Integer.valueOf(mapls.get("i")+""));
        budget.setYsId(Integer.valueOf(mapls.get("ysId")+""));
        budget.setBudgetName(mapls.get("budgetName")+"");
        budget.setPId(Integer.valueOf(mapls.get("pId")+""));
        budget.setEId(Integer.valueOf(mapls.get("eId")+""));
        budget.setCreateDate(mapls.get("createDate")+"");
        budget.setComment(mapls.get("comment")+"");
        budget.setAttachIds(mapls.get("attachIds")+"");
        budget.setStatus(Integer.valueOf(1+""));
        iBudgetService.saveOrUpdate(budget);
        List<Map<String,Object>> maptpd = (List<Map<String, Object>>) adds.get("tenderPurchaseDetails");
        List<Directly> aiList = Lists.newArrayList();
        maptpd.forEach(i->{
            Directly qq = new Directly();
            qq.setId(Integer.valueOf(i.get("id")+""));
            qq.setCourseName(i.get("courseName")+"");
            qq.setMoney(BigDecimal.valueOf(Double.valueOf(i.get("money")+"")));
            qq.setComment(i.get("comment")+"");
            qq.setMoneyCapital(i.get("moneyCapital")+"");
            qq.setBId(budget.getI());
            aiList.add(qq);
        });
        directlyService.saveBatch(aiList);
        return ResultTool.success();
    }

    @PutMapping("/aa")
    public Result updateSubcontract1(@RequestBody  Map<String,Object> adds){
        System.out.println(adds);
        Budget budget = new Budget();
        Map<String,Object> mapls = (Map<String, Object>) adds.get("subcontracts");
        budget.setI(Integer.valueOf(mapls.get("i")+""));
        budget.setYsId(Integer.valueOf(mapls.get("ysId")+""));
        budget.setBudgetName(mapls.get("budgetName")+"");
        budget.setPId(Integer.valueOf(mapls.get("pId")+""));
        budget.setEId(Integer.valueOf(mapls.get("eId")+""));
        budget.setCreateDate(mapls.get("createDate")+"");
        budget.setComment(mapls.get("comment")+"");
        budget.setAttachIds(mapls.get("attachIds")+"");
        budget.setStatus(Integer.valueOf(1+""));
        iBudgetService.saveOrUpdate(budget);
        List<Map<String,Object>> maptpd = (List<Map<String, Object>>) adds.get("tenderPurchaseDetails");
        List<Indirect> aiList = Lists.newArrayList();
        maptpd.forEach(i->{
            Indirect qq = new Indirect();
            qq.setId(Integer.valueOf(i.get("id")+""));
            qq.setCourseName(i.get("courseName")+"");
            qq.setMoney(BigDecimal.valueOf(Double.valueOf(i.get("money")+"")));
            qq.setComment(i.get("comment")+"");
            qq.setMoneyCapital(i.get("moneyCapital")+"");
            qq.setBId(budget.getI());
            aiList.add(qq);
        });
        iIndirectService.saveBatch(aiList);
        return ResultTool.success();
    }

    @PostMapping("/aa")
    public Result addLaborSubcontract1(@RequestBody  Map<String,Object> adds){
        System.out.println(adds);
        Budget budget = new Budget();
        Map<String,Object> mapls = (Map<String, Object>) adds.get("subcontracts");
        budget.setYsId(Integer.valueOf(mapls.get("ysId")+""));
        budget.setBudgetName(mapls.get("budgetName")+"");
        budget.setPId(Integer.valueOf(mapls.get("pId")+""));
        budget.setEId(Integer.valueOf(mapls.get("eId")+""));
        budget.setCreateDate(mapls.get("createDate")+"");
        budget.setComment(mapls.get("comment")+"");
        budget.setAttachIds(mapls.get("attachIds")+"");
        budget.setStatus(Integer.valueOf(1+""));
        iBudgetService.saveOrUpdate(budget);
        List<Map<String,Object>> maptpd = (List<Map<String, Object>>) adds.get("tenderPurchaseDetails");
        List<Indirect> aiList = Lists.newArrayList();
        maptpd.forEach(i->{
            Indirect qq = new Indirect();
            qq.setCourseName(i.get("courseName")+"");
            qq.setMoney(BigDecimal.valueOf(Double.valueOf(i.get("money")+"")));
            qq.setComment(i.get("comment")+"");
            qq.setMoneyCapital(i.get("moneyCapital")+"");
            qq.setBId(budget.getI());
            aiList.add(qq);
        });
        iIndirectService.saveBatch(aiList);
        return ResultTool.success();
    }

    @DeleteMapping("/{i}")
    public Result deleteByIdSubcontract(@PathVariable String i){
        iBudgetService.removeById(i);
        directlyService.remove(new QueryWrapper<Directly>().eq("b_id",i));
        return ResultTool.success();
    }

    @DeleteMapping("/aa/{i}")
    public Result deleteByIdSubcontract1(@PathVariable String i){
        iBudgetService.removeById(i);
        iIndirectService.remove(new QueryWrapper<Indirect>().eq("b_id",i));
        return ResultTool.success();
    }

    @GetMapping("/{id}")
    public Result getByIdDirectly(@PathVariable String id){
        Map<String, Object> map =new HashMap<>();
        //查询所有单位类型
        Budget budget = iBudgetService.listTo(id);
        map.put("budget", budget);
        //查询所有单位
        List<Directly> il = directlyService.list(new QueryWrapper<Directly>().eq("b_id", id));
        map.put("directly", il);
        return ResultTool.success(map);
    }

    @GetMapping("/cc/{id}")
    public Result getByIdDirectly1(@PathVariable String id){
        Map<String, Object> map =new HashMap<>();
        //查询所有单位类型
        Budget budget = iBudgetService.listTo(id);
        map.put("budget", budget);
        //查询所有单位
        List<Indirect> il = iIndirectService.list(new QueryWrapper<Indirect>().eq("b_id", id));
        map.put("indirect", il);
        return ResultTool.success(map);
    }
}
